﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///Developed By Indie Studio
///https://assetstore.unity.com/publishers/9268
///www.indiestd.com
///info@indiestd.com

namespace IndieStudio.BugsBang.Game
{
	[DisallowMultipleComponent]
	public class Cursor : MonoBehaviour
	{
		/// <summary>
		/// Whether the cursor is enabled or not.
		/// </summary>
		public bool isEnabled = true;

		/// <summary>
		/// The trails renderer of the cursor.
		/// </summary>
		public GameObject [] trailsRenderers;

		/// <summary>
		/// The shadow of the cursor.
		/// </summary>
		public SpriteRenderer shadow;

		/// <summary>
		/// The position of the cursor.
		/// </summary>
		private Vector3 pos;

		/// <summary>
		/// This Gameobject defined as a Singleton.
		/// </summary>
		public static Cursor instance;

		void Awake ()
		{
			if (instance == null) {
				instance = this;
				DontDestroyOnLoad (gameObject);
			} else {
				Destroy (gameObject);
			}
		}

		// Use this for initialization
		void Start ()
		{
			if (!isEnabled || Application.isMobilePlatform) {
				//Disable cursor in mobile platform
				GetComponent<SpriteRenderer> ().enabled = false;
				shadow.gameObject.SetActive(false);
			} 
		}
		
		// Update is called once per frame
		void Update ()
		{
			if(isEnabled)
				DrawCursor ();
		}

		/// <summary>
		/// Draw the cursor on screen.
		/// </summary>
		private void DrawCursor(){
			pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
			pos.z = 0;
			transform.position = pos;
			UnityEngine.Cursor.visible = false;//hide default cursor
		}

		/// <summary>
		/// Enable the trail of the cursor.
		/// </summary>
		public void EnableTrail(int index){

            if (!(index >= 0 && index < trailsRenderers.Length))
            {
                return;
            }

			if(isEnabled)
                trailsRenderers[index].SetActive(true);
		}

		/// <summary>
		/// Disable the trail of the cursor.
		/// </summary>
		public void DisableTrail(int index){

            if (!(index >= 0 && index < trailsRenderers.Length))
            {
                return;
            }

            if (trailsRenderers[index] != null)
                trailsRenderers[index].SetActive(false);
		}


        /// <summary>
        /// Disable all trails
        /// </summary>
        public void DisableTrails()
        {
            for (int i = 0; i < trailsRenderers.Length; i++)
            {
                DisableTrail(i);
            }
        }
	}
}
